package com.kkks.spring_rest.tick.restcontroller;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kkks.spring_rest.tick.model.pojo.WhoUserLogin;
import com.kkks.spring_rest.tick.service.UserService;
import com.kkks.spring_rest.tick.utils.GsonHelper;
import com.kkks.spring_rest.tick.utils.LogHelper;
import com.kkks.spring_rest.tick.utils.LogOutput;



/**
 * @author SC
 * @version 1.0
 * @created 2012-6-28 下午01:16:25
 * @describe
 * @RequestMapping("/UserRestController") 具有层次关系，方法级的将在类一级@RequestMapping之一,
 *                                        如下面示例,
 *                                        访问方法级别的@RequestMapping("/new"),则URL为
 *                                        /user/new
 */

@Controller
@RequestMapping("/UserRestController")
public class UserRestController {

	@Autowired
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 使用 @RequestParam 将 URL 参数插入方法中。 使用 @RequestHeader 将某一 HTTP 头插入方法中。 使用 @RequestBody将
	 * HTTP 请求正文插入方法中。 使用 @ResponseBody 将内容或对象作为 HTTP 响应正文返回。 使用
	 * HttpEntity<T>将它自动插入方法中，如果将它作为参数提供。 使用 ResponseEntity<T> 返回具有自定义状态或头的 HTTP
	 * 响应。 描述 注释
	 */

	/**
	 * 描述 用户登录类
	 * 
	 * @param username
	 * @param password
	 * @return WhoUserLogin
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "/userLogin.do/{userName}/{password}", method = RequestMethod.GET, headers = "Accept=text/html")
	@ResponseBody
	public String UserLogin(@PathVariable("userName") String username,
			@PathVariable("password") String password) {

		LogHelper.logInput(LogOutput.LogShow("in", "userLogin.do",
				new Object[] { "userName", "password" }, new Object[] {
						username, password }));
		String result = "";
		try {
			WhoUserLogin whoUserLogin = this.userService.UserLogin(username,
					password);
			String returnValue = GsonHelper.getJsonString(whoUserLogin);
			LogHelper.logOutput("userLogin.do" + returnValue);

			result = java.net.URLEncoder.encode(returnValue, "utf-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/userRegister.do/{userName}/{password}/{confirm}/{phoneType}", method = RequestMethod.GET, headers = "Accept=application/json")
	@ResponseBody
	public String UserRegister(@PathVariable("userName") String userName,
			@PathVariable("password")String password, @PathVariable("confirm") String confirm, @PathVariable("phoneType")String phoneType) {
//		LogHelper.logInput(LogOutput.LogShow("in", "UserRegister.do",
//				new Object[] { "userName", "password", "confirm", "phoneType" }, new Object[] {
//				userName, password, confirm, phoneType }));
			String result = null;
		try {
			Map map = this.userService.UserRegister(userName, password, confirm, phoneType);
			String returnValue = GsonHelper.getJsonString(map);
			LogHelper.logOutput("UserRegister.do" + returnValue);

			result = returnValue;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return result;
	}

	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/{username}/{password}/{age}/{sex}", method = RequestMethod.GET, headers = "Accept=application/json")
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	@ResponseBody
	public Map saveUser(@PathVariable("username") String username,
			@PathVariable("password") String password,
			@PathVariable("age") String age, @PathVariable("sex") Integer sex)
			throws Exception {

		return null;
	}

}
